package de.hdu.myjobapp.dao;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import de.hdu.myjobapp.model.User;

/**
 * Vorbereitet fuer eine weitere Implementierung
 */	
public class UserDao extends HibernateDaoSupport{
	
	/**
	 * create a new User
	 */	
	public void create(String userLogin, String userName, String userPassword){
		User u = new User(userLogin,userName,userPassword);
		//u.setUserLogin(userLogin);
		//u.setUserName(userName);
		//u.setUserPassword(userPassword);
		save(u);
	}

	/**
	 * Saves the new recieved User from Method create with hibernate into database
	 */	
	public User save(User user){
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(user);
		return user;
	}

	/**
	 * return all existing Status
	 */	
	public List<User> findAll(){
		HibernateTemplate template = getHibernateTemplate();
		return template.loadAll(User.class);
	}

	/**
	 * Return an existing Rating by naming ID
	 */	
	public User findById(int id){
		HibernateTemplate template = getHibernateTemplate();
		return(User) template.get(User.class, id);
	}
	
	@SuppressWarnings("unchecked")
	public User findByName(String userLogin){
		User find = new User();
		HibernateTemplate template = getHibernateTemplate();
		
		List <User> aUser = template.find("from User u where u.userLogin = '" + userLogin + "'");
		for (User xUser : aUser) {
			find = xUser;
		}
		
		return find;
		
	}


}



